<template>
  <div class="update-name">
    <!-- 导航栏 -->
    <van-nav-bar
    title="设置昵称"
    left-text="取消"
    right-text="完成"
    @click-left="$emit('close')"
    @click-right="onUpdate"
    />
    <!-- /导航栏 -->
    <div class="field">
    <!-- 输入框 -->
    <van-field
  v-model="message"
  rows="2"
  autosize
  type="textarea"
  maxlength="7"
  placeholder="请输入昵称"
  show-word-limit
/>
</div>
    <!-- /输入框 -->

  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  data () {
    return {
      message: this.value
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    async onUpdate () {
      try {
        const message = this.message
        if (!message.length) {
          this.$toast('昵称不能为空')
          return
        }
        await updateUserProfile({
          name: message
        })
        // 更新视图
        this.$emit('input', message)
        // 关系弹出()
        this.$emit('close')
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.field{
    padding:20px
}
</style>
